//
//  NewsModel.swift
//  SCA
//
//  Created by tjun on 2025/9/3.
//

import UIKit

class NewsModel {
    let title: String
    var attributedText: NSAttributedString?
    
    init(title: String, content: String) {
        if let data = content.data(using: .utf8) {
            let options: [NSAttributedString.DocumentReadingOptionKey: Any] = [
                .documentType: NSAttributedString.DocumentType.html,
                .characterEncoding: String.Encoding.utf8.rawValue
            ]
            
            if let attributedText = try? NSMutableAttributedString(data: data, options: options, documentAttributes: nil) {
                let fullRange = NSRange(location: 0, length: attributedText.length)
                attributedText.addAttributes([
                    .font: UIFont.systemFont(ofSize: 14),
                    .foregroundColor: UIColor.lightText
                ], range: fullRange)
                self.attributedText = attributedText
            }
        }
        self.title = title
    }
}


class NewsListModel {
    
    let Id: Int
    let date: String
    let title: String
    let pageType: NewsType
    let displayType: String
    var url: String?
    var attributedText: NSAttributedString?
    
    init(Id: Int, date: String, title: String, pageType: NewsType, displayType: String, url: String?, content: String) {
        if let data = content.data(using: .utf8) {
            let options: [NSAttributedString.DocumentReadingOptionKey: Any] = [
                .documentType: NSAttributedString.DocumentType.html,
                .characterEncoding: String.Encoding.utf8.rawValue
            ]
            
            if let attributedText = try? NSMutableAttributedString(data: data, options: options, documentAttributes: nil) {
                let fullRange = NSRange(location: 0, length: attributedText.length)
                attributedText.addAttributes([
                    .font: UIFont.systemFont(ofSize: 15),
                    .foregroundColor: UIColor.lightText
                ], range: fullRange)
                self.attributedText = attributedText
            }
        }
        self.Id = Id
        self.url = url
        self.displayType = displayType
        self.pageType = pageType
        self.title = title
        self.date = date
    }
}
